package com.gobang.gobangboot.config;

import com.gobang.gobangboot.interceptor.JwtTokenUserInterceptor;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
@Slf4j
public class WebMvcConfiguration extends WebMvcConfigurationSupport {

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new JwtTokenUserInterceptor())
                .addPathPatterns("/user/**")
                .excludePathPatterns("/user/login")
//                .excludePathPatterns("/user/updateAvatar")
                .excludePathPatterns("/user/register")
                .excludePathPatterns("/user/getCode")
                .excludePathPatterns("/user/loginByCode");
    }

    protected void addResourceHandlers (ResourceHandlerRegistry registry) {
        registry.addResourceHandler ("/doc.html").addResourceLocations ("classpath:/META-INF/resources/");
        registry.addResourceHandler ("/webjars/**").addResourceLocations ("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/avatar/**")
                .addResourceLocations("file:./avatar/");
    }

    @Bean
    public OpenAPI springShopOpenAPI() {
        return new OpenAPI()
                .info(new Info().title("五子棋文档")
                        .contact(new Contact())
                        .description("五子棋文档")
                        .version("v1"));
    }

}
